﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Modles.DTO;
using Modles.Models;

namespace BilbilDAL
{
    public class SearchForRecordDAL
    {
        /// <summary>
        /// 搜索记录持久化
        /// </summary>
        public void AddSearchForRecord(SearchForRecord_DTO dto)
        {
            using (VideoPlatformDbContext com = new VideoPlatformDbContext())
            {
                SearchForRecord searchForRecord = com.SearchForRecords.FirstOrDefault(x => x.Recording.Equals(dto.Recording));
                if (searchForRecord == null)
                {
                    com.SearchForRecords.Add(new SearchForRecord() { Recording = dto.Recording });
                }
                else 
                {
                    searchForRecord.Frequency += 1;
                }
                com.SaveChanges();
            }
        }
        /// <summary>
        /// 获取热门搜索
        /// </summary>
        /// <returns></returns>
        public List<SearchForRecord> GetSearchForRecords()
        {
            using (VideoPlatformDbContext com = new VideoPlatformDbContext())
            {
                return com.SearchForRecords.OrderByDescending(x => x.Frequency).Take(10).ToList();
            }
        }
    }
}
